#include <iostream>

using namespace std;
using ll=long long;
const int N=1e5+10;

struct node{
	int l,r;
	ll sum,add,mul;
}tr[N*4];
int n,p;
ll a[N];
void pushup(int u)
{
	tr[u].sum=(tr[u<<1].sum+tr[u<<1|1].sum)%p;
}

//a*b+c  *d+e
//a*b*d+c*d+e
void eval(int u,ll add,ll mul)
{
	tr[u].sum=(tr[u].sum*mul+(tr[u].r-tr[u].l+1)*add)%p;
	tr[u].add=(tr[u].add*mul+add)%p;
	tr[u].mul=(tr[u].mul*mul)%p;
}
void pushdown(int u)
{
	eval(u<<1,tr[u].add,tr[u].mul);
	eval(u<<1|1,tr[u].add,tr[u].mul);
	tr[u].add=0,tr[u].mul=1;	
}
void build(int u,int l,int r)
{
	if(l==r)
	{
		tr[u]={l,r,a[r],0,1};
	}
	else{
		
		tr[u]={l,r,0,0,1};
		int mid=l+r>>1;
		build(u<<1,l,mid);
		build(u<<1|1,mid+1,r);
		pushup(u);
	}
}

void modify(int u,int l,int r,ll add,ll mul)
{
	if(tr[u].l>=l && tr[u].r<=r)
	{
		eval(u,add,mul);
	}
	else{
		pushdown(u);
		int mid=tr[u].r+tr[u].l>>1;
		if(l<=mid)
			modify(u<<1,l,r,add,mul);
		if(r>mid)
			modify(u<<1|1,l,r,add,mul);
		pushup(u);
	}
}

ll query(int u,int l,int r)
{
	if(tr[u].l>=l && tr[u].r<=r)
		return tr[u].sum;
	pushdown(u);
	int mid=tr[u].l+tr[u].r>>1;
	ll ret=0;
	if(l<=mid)
	{
		ret=query(u<<1,l,r);
	}
	if(r>mid)
	{
		ret=(ret+query(u<<1|1,l,r))%p;
	}
	return ret;
}
int main()
{
	cin>>n>>p;
	for(int i=1;i<=n;i++)
		scanf("%d",&a[i]);
	build(1,1,n);
	int m;
	cin>>m;
	while(m--)
	{
		int op,l,r,c;
		scanf("%d%d%d",&op,&l,&r);
		if(op==1)
		{
			scanf("%d",&c);
			modify(1,l,r,0,c);
		}
		else if(op==2)
		{
			scanf("%d",&c);
			modify(1,l,r,c,1);			
		}
		else{
			cout<<query(1,l,r)<<endl;
		}
	}
	
	return 0;
}
